<?php

namespace App\Http\Controllers\Backend;

use App\Http\Requests\LoginRequest;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    /**
     * 显示登录表单
     *
     * @return \Illuminate\View\View
     */
    public function showLoginForm()
    {
        return view('backend.auth.login');
    }

    /**
     * 登录
     *
     * @param  \App\Http\Requests\LoginRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function login(LoginRequest $request)
    {
        $credentials = [
            'username' => $request->get('username'),
            'password' => $request->get('password'),
        ];

        if (Auth::attempt($credentials)) {
            return redirect()->intended(route('posts.index'));
        }

        return redirect()->route('login.form.show')
                         ->withInput($request->only('username'))
                         ->withErrors('用户名或密码错误。');
    }

    /**
     * 注销登录
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function logout()
    {
        Auth::logout();

        return redirect()->route('login.form.show');
    }
}
